<script setup lang="ts">
import { ref } from 'vue'
import type { TeacherItem } from '@/types/teacher.d.ts'
import { addTeacherAPI } from '@/pagesOrgan/services/teacher'
import { config } from '@/global'

// 表单数据
const formData = ref<TeacherItem>({
  teacherName: '',
  avatar: '',
  gender: '',
  teacherPhone: '',
  isEmployment: 'Y',
  //入职时间
  employmentDate: '',
})

// 提交表单
const submitForm = async () => {
  // 表单验证
  if (!formData.value.teacherName) {
    uni.showToast({ title: '请输入教师姓名', icon: 'none' })
    return
  }

  if (!formData.value.teacherPhone) {
    uni.showToast({ title: '请输入联系电话', icon: 'none' })
    return
  }
  if (!formData.value.employmentDate) {
    uni.showToast({ title: '请选择入职时间', icon: 'none' })
    return
  }

  try {
    await addTeacherAPI(formData.value)
    uni.showToast({ title: '添加成功' })

    // 通过事件总线通知上一页刷新数据
    uni.$emit('refreshTeacherList')

    // 返回列表页
    uni.navigateBack()
  } catch (error) {
    console.error('添加教师失败:', error)
    uni.showToast({ title: '添加教师失败', icon: 'none' })
  }
}
</script>

<template>
  <view class="viewport">
    <!-- 表单区域 -->
    <view class="form-container">
      <view class="form-content">
        <!-- 姓名 -->
        <view class="form-item">
          <text class="form-label">姓名</text>
          <input
            type="text"
            v-model="formData.teacherName"
            placeholder="请输入教师姓名"
            class="form-input"
          />
        </view>

        <!-- 性别 -->
        <view class="form-item">
          <text class="form-label">性别</text>
          <view class="radio-group">
            <uni-data-checkbox v-model="formData.gender" :localdata="config.genderList" />
          </view>
        </view>

        <!-- 电话 -->
        <view class="form-item">
          <text class="form-label">电话</text>
          <input
            type="number"
            v-model="formData.teacherPhone"
            placeholder="请输入联系电话"
            class="form-input"
          />
        </view>

        <!-- 入职时间 -->
        <view class="form-item">
          <text class="form-label">入职时间</text>
          <uni-datetime-picker
            type="date"
            v-model="formData.employmentDate"
            placeholder="选择入职时间"
          />
        </view>
      </view>

      <!-- 提交按钮 -->
      <button @click="submitForm" class="submit-button">提 交</button>
    </view>
  </view>
</template>

<style lang="scss">
page {
  background-color: #f4f4f4;
}

.viewport {
  display: flex;
  flex-direction: column;
  height: 100vh;
  background-size: auto 420rpx;
  background-repeat: no-repeat;
}

// 导航栏样式
.navbar {
  position: relative;
  height: 88rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  padding-top: env(safe-area-inset-top);
  background-color: transparent;

  .title {
    font-size: 36rpx;
    font-weight: 500;
    color: #fff;
  }

  .back {
    position: absolute;
    left: 24rpx;
    width: 44rpx;
    height: 44rpx;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 44rpx;
    color: #fff;
  }
}

// 头像区域样式
.avatar-section {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 260rpx;
  margin-top: -20rpx;

  .avatar-container {
    display: flex;
    flex-direction: column;
    align-items: center;

    .avatar-image {
      width: 160rpx;
      height: 160rpx;
      border-radius: 50%;
      border: 4rpx solid #fff;
      box-shadow: 0 4rpx 10rpx rgba(0, 0, 0, 0.1);
    }

    .avatar-text {
      margin-top: 16rpx;
      font-size: 26rpx;
      color: #fff;
    }
  }
}

// 表单区域样式
.form-container {
  padding: 0 24rpx;
  margin-top: 20rpx;

  .form-content {
    padding: 0 24rpx;
    border-radius: 16rpx;
    background-color: #fff;
    box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
  }

  .form-item {
    display: flex;
    flex-direction: column;
    padding: 30rpx 0;
    border-bottom: 1rpx solid #f0f0f0;

    &:last-child {
      border-bottom: none;
    }

    .form-label {
      margin-bottom: 16rpx;
      font-size: 28rpx;
      color: #333;
      font-weight: 500;
    }

    .form-input {
      width: 100%;
      height: 70rpx;
      padding: 0 16rpx;
      font-size: 28rpx;
      border: 1rpx solid #e0e0e0;
      border-radius: 6rpx;
      box-sizing: border-box;
    }

    .radio-group {
      display: flex;
      gap: 40rpx;
      padding: 10rpx 0;

      .radio {
        display: flex;
        align-items: center;
        font-size: 28rpx;
        color: #333;

        radio {
          margin-right: 8rpx;
        }
      }
    }
  }

  .submit-button {
    width: 100%;
    height: 90rpx;
    margin-top: 40rpx;
    margin-bottom: 40rpx;
    font-size: 32rpx;
    color: #fff;
    background-color: #27ba9b;
    border-radius: 45rpx;
    box-shadow: 0 4rpx 10rpx rgba(39, 186, 155, 0.3);
  }
}
</style>
